#ifndef _THREAD_H_
#define _THREAD_H_

#include <iostream>
#include <string>
#include <pthread.h>
#include <cstring>
#include <cstdio>
#include <functional>
namespace ThreadModule
{
    static uint32_t number = 1;
    class Thread
    {
        using func_t = std::function<void()>;

    public:
        Thread(func_t func) : _tid(0),
                                      _isdetach(false),
                                      _isrunning(false),
                                      res(nullptr),
                                      _func(func)
        {
            _name = "thread-" + std::to_string(number++);
        }

        void enabledetach()
        {
            _isdetach = true;
        }
        void detach() // 支持启动前后都可以分离
        {
            if (_isdetach)
                return;
            if (_isrunning)
                pthread_detach(_tid);
            enabledetach();
        }
        void enablerunning()
        {
            _isrunning = true;
        }
        static void *myroutine(void *args)//加上static关键字，否则会有隐含的this指针，导致参数不匹配
        {
            Thread *self = static_cast<Thread *>(args);
            self->enablerunning();
            if (self->_isdetach)
            {
                self->detach();
            }
            self->_func();
            return nullptr;
        }
        bool start() // 线程启动
        {
            if (_isrunning)
                return false;
            int n = pthread_create(&_tid, nullptr, myroutine, this); // 创建线程
            if (n > 0)                                                  // 成功返回零
            {
                std::cerr << "create thread error: " << strerror(n) << std::endl;
                return false;
            }
            else
            {
                std::cout << "success!!!" << std::endl;
                if (_isdetach)
                {
                    detach();
                }
                enablerunning();
                return true;
            }
        }
        bool stop()
        {
            if (_isrunning)
            {
                int n = pthread_cancel(_tid);
                if (n != 0)
                {
                    std::cerr << "cancel thread error: " << strerror(n) << std::endl;
                    return false;
                }
                else
                {
                    std::cout << _name << "success thread stop!!!" << std::endl;
                    _isrunning = false;
                    return true;
                }
            }
            return true;
        }
        void join()
        {
            if (_isdetach)
                return;
            int n = pthread_join(_tid, &res);
            if (n != 0)
            {
                std::cerr << "cancel thread error: " << strerror(n) << std::endl;
            }
            else
            {
                std::cout << "success!!! join the thread" << std::endl;
            }
        }
        std::string  name()
        {
            return _name;
        }
        ~Thread()
        {
        }

    private:
        pthread_t _tid;
        std::string _name;
        bool _isdetach;
        bool _isrunning;
        void *res;
        func_t _func;
    };
}
#endif